﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Media;
using InteControlSys.ViewModel;

namespace InteControlSys
{
    public class ChildTreeViewModel : INPCBase
    {
        //private Tree2ViewModel tree2VM;//子列表
        //public Tree2ViewModel Tree2VM
        //{
        //    get { return tree2VM; }
        //    set
        //    {
        //        tree2VM = value;
        //        NotifyChanged("Tree2VM");
        //    }
        //}

        public ProcessViewModel Parent { get; set; }
        

        public SimpleCommand ClearSelectedItemsCommand { get; private set; }
        public SimpleCommand SelectItemCommand { get; private set; }
        public SimpleCommand AddToSelectionCommand { get; private set; }
        public SimpleCommand RemoveFromSelectionCommand { get; private set; }
        public SimpleCommand SelectAllCommand { get; private set; }



        private ObservableCollection<NodeViewModel> rootChildren = new ObservableCollection<NodeViewModel> { };
        public ObservableCollection<NodeViewModel> RootChildren
        {
            get { return rootChildren; }
        }

        private PathGeometry _backgroudGrid;
        public PathGeometry BackgroudGrid
        {
            get { return _backgroudGrid; }
            set
            {
                _backgroudGrid = value;
                NotifyChanged("BackgroudGrid");
            }
        }

        public ChildTreeViewModel(ProcessViewModel parent)
        {
            this.Parent = parent;

            ClearSelectedItemsCommand = new SimpleCommand(ExecuteClearSelectedItemsCommand);
            SelectItemCommand = new SimpleCommand(ExecuteSelectItemCommand);
            AddToSelectionCommand = new SimpleCommand(ExecuteAddToSelectionCommand);
            RemoveFromSelectionCommand = new SimpleCommand(ExecuteRemoveFromSelectionCommand);
            SelectAllCommand = new SimpleCommand(ExecuteSelectAllCommand);
        }

        private NodeViewModel _selectedNode;
        public NodeViewModel SelectedNode
        {
            get { return _selectedNode; }
            set 
            { 
                _selectedNode = value;
                NotifyChanged("SelectedNode"); 
            }
        }

        public List<ISelectable> Items = new List<ISelectable>();
        private List<ISelectable> selectedItems = new List<ISelectable>();
        public List<ISelectable> SelectedItems
        {
            //we need the order of the selected items
            //get { return Items.Where(x => x.IsSelected).ToList(); }
            get { return selectedItems; }
            set { selectedItems = value; NotifyChanged("SelectedItems"); }
        }
        private void ExecuteSelectItemCommand(object parameter)
        {
            //ExecuteClearSelectedItemsCommand(null);

            //(parameter as ISelectable).IsSelected = true;
            ////this.SelectedItems.Add(parameter as ISelectable);//lis 2014.07.15
            //if (parameter.GetType() == typeof(DSItemNodeViewModel))
            //    (parameter as DSItemNodeViewModel).ExpandParents();

            //this.Parent.View._ConfigView.ScrollIntoView(parameter);
        }

        private void ExecuteClearSelectedItemsCommand(object parameter)
        {
            foreach (ISelectable item in Items)
            {
                item.IsSelected = false;
            }
            this.SelectedItems.Clear();
        }
        private void ExecuteSelectAllCommand(object parameter)
        {
            foreach (ISelectable item in Items)
            {
                item.IsSelected = true;
                //this.SelectedItems.Add(item);
            }
        }
        private void ExecuteAddToSelectionCommand(object parameter)
        {
            ISelectable item = (ISelectable)parameter;
            item.IsSelected = true;
            //this.SelectedItems.Add(item);
        }

        private void ExecuteRemoveFromSelectionCommand(object parameter)
        {
            ISelectable item = (ISelectable)parameter;
            item.IsSelected = false;
            //this.SelectedItems.Remove(item);
        }

#region Helper

        public String GetValidName(String caption,bool bCopy)
        {
            String tempCaption="";
            bool flag = true;
            //lis 2015.03.27  对象复制后粘贴到其他页面时如果没有重名的，名称可以保持不变
            if (bCopy)
            {
                flag = false;
                tempCaption = caption;
                foreach (NodeViewModel tempitem in this.Items.OfType<NodeViewModel>())
                {
                    if (tempitem.Name == tempCaption)
                    {
                        flag = true;//有重名，重新命名
                        break;
                    }
                }
            }

            int index = 0;
            //String tempCaption="";
            //bool flag = true;
            while (flag)
            {
                flag = false;
                if (bCopy)
                {
                    tempCaption = caption + "(" + (index++).ToString() + ")";
                }
                else
                {
                    tempCaption = caption + (index++).ToString();
                }
                foreach (NodeViewModel tempitem in this.Items.OfType<NodeViewModel>())
                {
                    if (tempitem.Name == tempCaption)
                    {
                        flag = true;
                        break;
                    }
                }
            }
            return tempCaption;
        }

        

#endregion


    }
}
